const express = require('express')
const fs = require('fs')
const app = express()

app.get('/', (req, res, next) => {
    throw new Error('发生错误')
})

app.use((err, req, res, next) => {
    fs.writeFile('./app.log', `${req.method} ${req.url} Error:${err.message}`, () => {
        next(err)
    })
})

app.use((err, req, res, next) => {
    res.json({
        path: req.path,
        message: err.message
    })
})

app.listen(8899, () => {
    console.log('listen on 8899');
})